Skip to content

Add YAML+Jinja lexer - #1268

Merged
alecthomas merged 1 commit into
alecthomas:masterfrom
ktrysmt:add-yaml-jinja-lexer
May 17, 2026
Merged

Add YAML+Jinja lexer#1268
alecthomas merged 1 commit into
alecthomas:masterfrom
ktrysmt:add-yaml-jinja-lexer

Conversation

@ktrysmt

@ktrysmt ktrysmt commented May 17, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds a YAML+Jinja lexer that combines the existing YAML and Django/Jinja lexers via DelegatingLexer. This is the equivalent of Pygments' YamlJinjaLexer and covers two common formats that the current lexers cannot highlight correctly on their own:

  • Ansible playbooks (Jinja2 expressions embedded in YAML)
  • Salt SLS files

Implementation follows the same pattern as ERB, PHTML, Svelte, and Go HTML Template: a small Go file wraps two existing lexers with DelegatingLexer, reusing embedded/yaml.xml and embedded/django_jinja.xml. No new XML file is introduced.

Lexer config

Field Value
Name YAML+Jinja
Aliases yaml+jinja, salt, sls, ansible
Filenames *.sls
MIME types text/x-yaml+jinja, text/x-sls

Why no *.yml / *.yaml?

Ansible playbooks and Salt SLS files often use *.yml. Claiming those globs would route every plain YAML file through this lexer and create an ambiguous match against the built-in YAML lexer. To stay non-invasive, the lexer only claims *.sls; for Ansible playbooks the user is expected to select the lexer explicitly (chroma -l ansible …), matching Pygments' behavior.

Existing lexers remain unaffected:

Input Resolved lexer
*.yml / *.yaml YAML (unchanged)
*.sls YAML+Jinja (new)
text/x-yaml YAML (unchanged)
application/x-jinja Django/Jinja (unchanged)
text/x-yaml+jinja, text/x-sls YAML+Jinja (new)

Test plan

  • go test ./... passes (new TestLexers/YAML+Jinja/testdata/ansible.actual case included; .expected generated via RECORD=true go test ./lexers)
  • golangci-lint run ./... passes
  • gofmt -l clean
  • Manual smoke test: lexers.Get("ansible"), lexers.Get("yaml+jinja"), lexers.Get("salt"), lexers.Get("sls") all resolve to the new YAML+Jinja lexer
  • Verified that *.yml, *.yaml, text/x-yaml, and application/x-jinja still resolve to the previous lexers (no regression)

Combines the existing YAML and Django/Jinja lexers via DelegatingLexer.
Equivalent to Pygments' YamlJinjaLexer; intended for Ansible playbooks
and Salt SLS files.

The lexer only claims `*.sls` as a filename glob so it does not steal
plain `*.yml` / `*.yaml` files from the YAML lexer. Use the `ansible`,
`yaml+jinja`, `salt`, or `sls` alias explicitly for Ansible playbooks.
@alecthomas

Copy link
Copy Markdown
Owner

Have you verified that it renders how you'd expect?

ktrysmt added a commit to ktrysmt/chroma that referenced this pull request May 17, 2026
@ktrysmt

ktrysmt commented May 17, 2026

Copy link
Copy Markdown
Contributor Author

Yes — I rendered lexers/testdata/ansible.actual with the new lexer at Monokai and the HTML formatter, and verified visually:

  • YAML structure (keys / scalars / booleans / numbers / list markers) is colored as plain YAML would be
  • Jinja delimiters ({{ }}, {% %}, {# #}) are tokenized as CommentPreproc / Comment and clearly separated from the surrounding YAML
  • Jinja keywords (if, endif), variables (ansible_user, package_name, inventory_hostname, …), filter pipe |, function call default(...), and inner string literals ('root') are all highlighted distinctly
  • Both quoted ("{{ x }}", "files/{{ x }}.pem") and unquoted (Ensure {{ x }} is installed, /etc/ssl/certs/{{ x }}.pem) embeddings work as expected
screenshot

@alecthomas
alecthomas merged commit 41fb546 into alecthomas:master May 17, 2026
2 checks passed
@alecthomas

Copy link
Copy Markdown
Owner

Thanks!

Maks1mS pushed a commit to stplr-dev/stplr that referenced this pull request May 29, 2026
…#442)

This PR contains the following updates:

| Package | Type | Update | Change | OpenSSF |
|---|---|---|---|---|
| [github.com/alecthomas/chroma/v2](https://github.com/alecthomas/chroma) | require | minor | `v2.24.1` → `v2.26.1` | [![OpenSSF Scorecard](https://api.securityscorecards.dev/projects/github.com/alecthomas/chroma/badge)](https://securityscorecards.dev/viewer/?uri=github.com/alecthomas/chroma) |

---

> ⚠️ **Warning**
>
> Some dependencies could not be looked up. Check the [Dependency Dashboard](issues/23) for more information.

---

### Release Notes

<details>
<summary>alecthomas/chroma (github.com/alecthomas/chroma/v2)</summary>

### [`v2.26.1`](https://github.com/alecthomas/chroma/releases/tag/v2.26.1)

[Compare Source](alecthomas/chroma@v2.26.0...v2.26.1)

#### Changelog

- [`56c7702`](alecthomas/chroma@56c7702) fix: downgrade go.mod version to 1.25

### [`v2.26.0`](https://github.com/alecthomas/chroma/releases/tag/v2.26.0)

[Compare Source](alecthomas/chroma@v2.25.0...v2.26.0)

#### Changelog

- [`a4d3f60`](alecthomas/chroma@a4d3f60) feat(chromad): use style counterparts for theme switching
- [`ce159e6`](alecthomas/chroma@ce159e6) chore: migrate to new bit format
- [`180ea9f`](alecthomas/chroma@180ea9f) perf(colour): replace Sprintf/ParseUint round-trip in NewColour with direct bit arithmetic ([#&#8203;1274](alecthomas/chroma#1274))
- [`68a08b0`](alecthomas/chroma@68a08b0) docs: how to support dynamic theme switching
- [`6fb9d92`](alecthomas/chroma@6fb9d92) feat(html): tag output with style mode
- [`a71fea3`](alecthomas/chroma@a71fea3) feat(styles): add light/dark mode support

### [`v2.25.0`](https://github.com/alecthomas/chroma/releases/tag/v2.25.0)

[Compare Source](alecthomas/chroma@v2.24.1...v2.25.0)

#### Changelog

- [`c3826f0`](alecthomas/chroma@c3826f0) chore: go mod tidy
- [`fb5bc39`](alecthomas/chroma@fb5bc39) fix: emit HTTP body tokens without Coalesce
- [`a3c2946`](alecthomas/chroma@a3c2946) Improve Nu file detection ([#&#8203;1260](alecthomas/chroma#1260))
- [`e841b1a`](alecthomas/chroma@e841b1a) chore(deps): update all non-major dependencies ([#&#8203;1272](alecthomas/chroma#1272))
- [`3ed2db8`](alecthomas/chroma@3ed2db8) Add Gemfile.lock lexer (& ruby improvements) ([#&#8203;1269](alecthomas/chroma#1269))
- [`41fb546`](alecthomas/chroma@41fb546) Add YAML+Jinja lexer ([#&#8203;1268](alecthomas/chroma#1268))
- [`e99b881`](alecthomas/chroma@e99b881) chore(deps): update all non-major dependencies ([#&#8203;1263](alecthomas/chroma#1263))
- [`e67dd2f`](alecthomas/chroma@e67dd2f) (Markless) Fix parse issue for embed directives without options ([#&#8203;1266](alecthomas/chroma#1266))
- [`dffa370`](alecthomas/chroma@dffa370) fix(go): tokenize trailing // as comment instead of consuming next line ([#&#8203;1265](alecthomas/chroma#1265))
- [`1cf1560`](alecthomas/chroma@1cf1560) chore: upgrade to github.com/dlclark/regexp2/v2
- [`2cbcf7b`](alecthomas/chroma@2cbcf7b) chore: upgrade golangci-lint
- [`786675b`](alecthomas/chroma@786675b) chore(deps): update all non-major dependencies ([#&#8203;1257](alecthomas/chroma#1257))
- [`235590c`](alecthomas/chroma@235590c) feat: add JSONL support to JSON lexer ([#&#8203;1262](alecthomas/chroma#1262))
- [`f9b5c97`](alecthomas/chroma@f9b5c97) fix(dart): match single-line comments without trailing newline ([#&#8203;1225](alecthomas/chroma#1225)) ([#&#8203;1261](alecthomas/chroma#1261))
- [`097f8e9`](alecthomas/chroma@097f8e9) Mention Arturo in README ([#&#8203;1256](alecthomas/chroma#1256))
- [`d46ce60`](alecthomas/chroma@d46ce60) feat(markdown): highlight frontmatter and comments ([#&#8203;1245](alecthomas/chroma#1245))
- [`f786b2a`](alecthomas/chroma@f786b2a) feat(lexers): add support for LilyPond ([#&#8203;1255](alecthomas/chroma#1255))
- [`0a02b98`](alecthomas/chroma@0a02b98) chore(deps): update actions/checkout digest to [`de0fac2`](alecthomas/chroma@de0fac2) ([#&#8203;1212](alecthomas/chroma#1212))
- [`c55009e`](alecthomas/chroma@c55009e) Fix AGENTS.md referencing a non-existent scripts directory ([#&#8203;1231](alecthomas/chroma#1231))
- [`c5e763e`](alecthomas/chroma@c5e763e) Improve protobuf lexer ([#&#8203;1253](alecthomas/chroma#1253))
- [`113cd0e`](alecthomas/chroma@113cd0e) Add Arturo lexer ([#&#8203;1232](alecthomas/chroma#1232))
- [`4498d71`](alecthomas/chroma@4498d71) chore(deps): update dependency binaryen to v129 ([#&#8203;1238](alecthomas/chroma#1238))
- [`885f912`](alecthomas/chroma@885f912) Added f4 to "Projects using Chroma" list ([#&#8203;1242](alecthomas/chroma#1242))
- [`c42c9ef`](alecthomas/chroma@c42c9ef) Update java lexer ([#&#8203;1254](alecthomas/chroma#1254))

</details>

---

### Configuration

📅 **Schedule**: (UTC)

- Branch creation
  - At 12:00 AM through 04:59 AM and 10:00 PM through 11:59 PM, Monday through Friday (`* 0-4,22-23 * * 1-5`)
  - Only on Sunday and Saturday (`* * * * 0,6`)
- Automerge
  - At any time (no schedule defined)

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My4xOTUuMyIsInVwZGF0ZWRJblZlciI6IjQzLjE5NS4zIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJLaW5kL0RlcGVuZGVuY2llcyJdfQ==-->

Reviewed-on: https://altlinux.space/stapler/stplr/pulls/442
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants